package com.zhentao.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.dto.h5.ProductSkuDto;
import com.zhentao.dto.product.SkuSaleDto;
import com.zhentao.entity.product.Product;
import com.zhentao.entity.product.ProductDetails;
import com.zhentao.entity.product.ProductSku;
import com.zhentao.mapper.ProductDetailsMapper;
import com.zhentao.mapper.ProductSkuMapper;
import com.zhentao.service.ProductService;
import com.zhentao.mapper.ProductMapper;
import com.zhentao.vo.h5.ProductItemVo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author Lenovo
* @description 针对表【product(商品)】的数据库操作Service实现
* @createDate 2024-09-03 20:01:27
*/
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product>
    implements ProductService{

    @Resource
    private ProductSkuMapper productSkuMapper;
    // 接口实现类
    @Resource
    private ProductMapper productMapper;

    @Resource
    private ProductDetailsMapper productDetailsMapper;
    /**
     * 获取畅销商品列表
     * @return
     */
    @Override
    public List<ProductSku> findProductSkuBySale() {
        return productSkuMapper.findProductSkuBySale();
    }

    @Override
    public Map<String, Object> findByPage(Integer page, Integer limit, ProductSkuDto productSkuDto) {
        Map<String,Object> map = new HashMap<>();
        Page<ProductSku> page1 = new Page<>(page,limit);
        Page<ProductSku> productSkuList = productSkuMapper.findByPage(page1,productSkuDto);
        List<ProductSku> records = productSkuList.getRecords();
        map.put("list",records);
        map.put("total",productSkuList.getTotal());
        return map;
    }
    /**
     *商品详情
     * @param skuId
     * @return
     */
    @Override
    public ProductItemVo item(Long skuId) {
//当前sku信息
        ProductSku productSku = productSkuMapper.selectById(skuId);

        //当前商品信息
        Product product = productMapper.selectOne(Wrappers.<Product>lambdaQuery().eq(Product::getId,productSku.getProductId()));

        //同一个商品下面的sku信息列表
        List<ProductSku> productSkuList = productSkuMapper.selectList(Wrappers.<ProductSku>lambdaQuery().eq(ProductSku::getProductId,productSku.getProductId()));
        //建立sku规格与skuId对应关系
        Map<String,Object> skuSpecValueMap = new HashMap<>();
        productSkuList.forEach(item -> {
            skuSpecValueMap.put(item.getSkuSpec(), item.getId());
        });

        //商品详情信息
        ProductDetails productDetails = productDetailsMapper.selectOne(Wrappers.<ProductDetails>lambdaQuery().eq(ProductDetails::getProductId,productSku.getProductId()));

        ProductItemVo productItemVo = new ProductItemVo();
        productItemVo.setProductSku(productSku);
        productItemVo.setProduct(product);
        productItemVo.setDetailsImageUrlList(Arrays.asList(productDetails.getImageUrls().split(",")));
        productItemVo.setSliderUrlList(Arrays.asList(product.getSliderUrls().split(",")));
        productItemVo.setSpecValueList(JSON.parseArray(product.getSpecValue()));
        productItemVo.setSkuSpecValueMap(skuSpecValueMap);
        return productItemVo;
    }
    /**
     * 获取商品sku信息
     * @param skuId
     * @return
     */
    @Override
    public ProductSku getBySkuId(Long skuId) {
        return productSkuMapper.selectById(skuId);
    }
    /**
     * 更新商品sku销量
     * @param skuSaleDtoList
     * @return
     */
    @Transactional
    @Override
    public Boolean updateSkuSaleNum(List<SkuSaleDto> skuSaleDtoList) {
        if(!CollectionUtils.isEmpty(skuSaleDtoList)) {
            for(SkuSaleDto skuSaleDto : skuSaleDtoList) {
                productSkuMapper.updateSale(skuSaleDto.getSkuId(), skuSaleDto.getNum());
            }
        }
        return true;
    }
}




